package com.whately.core.base.verifycode;

import com.whately.core.base.annotation.IgnoreSession;
import com.whately.core.base.model.ApiResponse;
import com.whately.core.base.verifycode.util.WhatelyVerifyCodeUtil;
import com.whately.core.base.verifycode.vo.WhatelyImgVerifyRespVo;
import com.whately.core.base.verifycode.vo.WhatelySliderVerifyRespVo;
import com.whately.core.base.verifycode.vo.req.FetchVerifyCodeReqVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 验证码
 * 包括图片验证码 ，短信验证码等
 */
@RestController
@RequestMapping("/api")
@Slf4j
@Api(tags  = "验证码-图片")
//@ApiSort(9999001)
public class WhatelyVerifyCodeImageController {

    @Autowired
    private WhatelyVerifyCodeUtil whatelyVerifyCodeUtil;


    /**
     * 获取注册验证码图片
     * 通过（token ，value）存redis
     * 返回token 和 img
     *
     * 如何防止被刷，导致redis爆掉？
     * nginx接口限流
     */
    @PostMapping(params = "method=whately.core.verifyCode.getImg")
    @IgnoreSession
    @ApiOperation("获取图形验证码")
    public ApiResponse<WhatelyImgVerifyRespVo> getImgVerifyCode(@RequestBody FetchVerifyCodeReqVo requestVo) {
        Integer type = requestVo.getType();
        WhatelyImgVerifyRespVo respVo = whatelyVerifyCodeUtil.getImgVerifyCodeRespVO(type);
        return ApiResponse.<WhatelyImgVerifyRespVo>builder().data(respVo).build();

    }

    /**
     * 获取注册验证码图片
     * 通过（token ，value）存redis
     * 返回token 和 img
     *
     * 如何防止被刷，导致redis爆掉？
     * nginx接口限流
     */
    @PostMapping(params = "method=whately.core.verifyCode.getSliderImg")
    @IgnoreSession
    @ApiOperation("获取滑块验证码")
    public ApiResponse<WhatelySliderVerifyRespVo> getSliderImg(@RequestBody FetchVerifyCodeReqVo requestVo) {
        Integer type = requestVo.getType();
        WhatelySliderVerifyRespVo vo = whatelyVerifyCodeUtil.getSliderImg(type);
        return  ApiResponse.<WhatelySliderVerifyRespVo>builder().data(vo).build();
    }

}
